package seehope.reflect;

import java.lang.reflect.Constructor;

public class ReflectionDemo02 {
    public static void main(String[] args) throws Exception{
        Class<Person> class4 = Person.class;
        System.out.println("------遍历所有的public修饰的构造方法------");
        //获得所有的构造器（代表所有的构造方法）
        Constructor[] constructors = class4.getConstructors();
        for(Constructor constructor:constructors){
            System.out.println("构造方法" + constructor);
            Class[] types = constructor.getParameterTypes();
            for(int i = 0; i < types.length; i ++ ){
                System.out.println("参数" + (i + 1) + "类型:" + types[i]);
            }
            System.out.println("");
        }

        System.out.println("------遍历所有的的构造方法------");
        Constructor[] constructors2 = class4.getDeclaredConstructors();
        for(Constructor constructor:constructors2){
            System.out.println("构造方法修饰符：" + (constructor.getModifiers() == 1 ? "public" : "private"));
            Class[] types = constructor.getParameterTypes();
            for(int i = 0; i < types.length; i ++ ){
                System.out.println("参数" + (i + 1) + "类型：" + types[i]);
            }
            System.out.println("");
        }

        System.out.println("------访问特定的构造方法------");
        System.out.println("---------无参---------");
        Constructor c1 = class4.getConstructor();
        Person p1 = (Person) c1.newInstance();
        System.out.println(p1);
        System.out.println("---------有参---------");
        Constructor c2 = class4.getConstructor(String.class, int.class);
        Person p2 = (Person) c2.newInstance("李四", 19);
        System.out.println(p2);

    }
}
